﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;

namespace WPFExcelReport
{
    public partial class TIVKPR1 : ViewBase
    {
       
        #region ==dwIVKPR1===========
        public static readonly DependencyProperty dwIVKPR1Property = DependencyProperty.Register("dwIVKPR1",
            typeof(DataRow), typeof(TIVKPR1),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwIVKPR1Changed));
        public DataRow dwIVKPR1
        {
            get
            {
                return (DataRow)GetValue(dwIVKPR1Property);
            }
            set
            {
                SetValue(dwIVKPR1Property, value);
            }
        }
        private static void OndwIVKPR1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVKPR1;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwIVKPR1 = row;
            } 
        }
        #endregion
       
        VVKPOS GB;
        public TIVKPR1()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_VKPRICE();
            GB = new VVKPOS();
            IsMasterFromMV = true;
             InitializeComponent();
           
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            base.Window_Loaded(sender, e);
            txtART.Focus();
        }        
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
                txtART.Focus();
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;          
            dwData = GB.SetProd.Tables["VKPOS"].NewRow(); 
            if (ART == "")
            {
                MessageBox.Show("请先输入任务书号！");
                return;
            } 
            dwData["VKPART"] =  Helper.Left(ART + "                        ", 24);
            try
            {
                string TYPE = "", WARPUSAGE = "", WEFTUSAGE = "";
                double WUSAGE = 0.00,EUSAGE = 0.00;
                Result = ((VICC_VKPRICE)MV).QueryICC_VKPRICE(dwData, 6);
                foreach (DataRow dw in Result.Tables["ICC_VKPRICE"].Rows)
                { 
                    TYPE = dw["TYPE"].ToString().Trim();
                    WARPUSAGE = dw["WARPUSAGE"].ToString().Trim();
                    WEFTUSAGE = dw["WEFTUSAGE"].ToString().Trim();
                    if(TYPE == ">3000")
                    {
                        WUSAGE = System.Convert.ToDouble(WARPUSAGE)*1;
                        EUSAGE = System.Convert.ToDouble(WEFTUSAGE)*1;
                    }
                    else if (TYPE=="2000-3000")
                    {
                        WUSAGE = System.Convert.ToDouble(WARPUSAGE) * 1.1;
                        EUSAGE = System.Convert.ToDouble(WEFTUSAGE) * 1.1;
                    }
                    else if (TYPE == "1000-2000")
                    {
                        WUSAGE = System.Convert.ToDouble(WARPUSAGE) * 1.2;
                        EUSAGE = System.Convert.ToDouble(WEFTUSAGE) * 1.2;
                    }
                    else if (TYPE == "500-1000")
                    {
                        WUSAGE = System.Convert.ToDouble(WARPUSAGE) * 1.5;
                        EUSAGE = System.Convert.ToDouble(WEFTUSAGE) * 1.5;
                    }
                    else
                    {
                        WUSAGE = System.Convert.ToDouble(WARPUSAGE) * 2;
                        EUSAGE = System.Convert.ToDouble(WEFTUSAGE) * 2;
                    }
                    dw["WARPUSAGE"] = Helpers.Helper.RoundX(WUSAGE, 10000);
                    dw["WEFTUSAGE"] = Helpers.Helper.RoundX(EUSAGE, 10000);
                }
                MergeData(Result,false); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwIVKPR1 = dwMaster;
        }
    }
}
